package com.hd.hdxgddcommon.utils.location;


import com.alibaba.fastjson.JSONObject;

import java.util.LinkedList;
import java.util.List;

public class LocationUtils {

    private static JSONObject icons; //地址图标

    public static String LOCATION_ICON = "LOCATION_ICON";

    public static String CONN_CHAR = "-";

    public static String ROOT_ID = "0000";

    public static String DEFAULT_ID = "0001";

    public static Integer CIRCLE = 4;

    /**
     * 初始化地址
     */
    public static void init() {
    }

    /**
     * 从数据库读取对应图标信息获取图标
     */
    public static void setIcons(JSONObject json) {
        icons = json;
    }

    /**
     * 通过location的level获取图标
     */
    public static String getIcon(Integer level) {
        return (String) icons.get(level);
    }


    /**
     * id 自增
     *
     * @param realId
     * @return
     */
    public static String createRealId(String realId) {
        String currentId;
        if (realId.contains(CONN_CHAR)) {
            int length = realId.length() - 5;
            currentId = realId.substring(0, length) + CONN_CHAR +
                    String.format("%04d", Integer.parseInt(realId.substring(length + 1)) + 1);
        } else {
            currentId = String.format("%04d", Integer.parseInt(realId) + 1);
        }
        return currentId;
    }

    /**
     * in 查询时 有效的Id集合
     */
    public static List<String> getValidIds(String locationId) {
        String replace = locationId.replace(CONN_CHAR, "");
        char[] chars = locationId.toCharArray();
        int total = replace.length() / CIRCLE;
        LinkedList<String> result = new LinkedList<>();
        for (Integer i = 0; i < total; i++) {
            result.add(new String(chars, 0, (i + 1) * CIRCLE + i));
        }
        return result;
    }

}
